<?php
/**
 * vim: tabstop=4
 * 
 * @license		http://www.gnu.org/licenses/gpl.html GPL Version 3
 * @author		Ian Moore <imooreyahoo@gmail.com>
 * @copyright	Copyright (c) 2011 Ian Moore
 * @version		$Id: localnotifications.inc 81 2011-12-13 18:25:04Z imooreyahoo@gmail.com $
 *
 * This file is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This file is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this file. If not, see <http://www.gnu.org/licenses/>.
 */
require_once("openmediavault/object.inc");
require_once("openmediavault/error.inc");
require_once("openmediavault/util.inc");
require_once("openmediavault/rpc.inc");

class LocalNotificationsRpc extends OMVRpc {


    /**
     * Verify that the current user is an admin, and validate method args
     */
    private function _validate($mname='',$args=array()) {

        // Check permissions
        $this->validateSession();
        if (!$this->hasRole(OMV_ROLE_ADMINISTRATOR)) {
            throw new OMVException(OMVErrorMsg::E_RPC_SERVICE_INVALID_PERMISSION);
        }
        $this->commitSession();

        // Check incoming data
        if($mname)
            $this->validateParams($mname, $args);
    }

	/**
	 * Interface with omvmbox.py script
	 */
	private function _mbox($fn, $args=array()) {

		if(count($args)) {
			$args = implode(" ", array_map('escapeshellarg', $args));
		} else {
			$args = '';
		}
		
		$cmd = "sudo /usr/local/bin/omvmbox.py {$fn} {$args} 2>&1";
		OMVUtil::exec($cmd, $output, $result);
		if ($result !== 0) {
			throw new OMVException(OMVErrorMsg::E_EXEC_FAILED,
			  $cmd, implode("\n", $output));
		}
		
		$obj = json_decode(implode("\n",$output), true);
		
		if(!empty($obj['error'])) {
			throw new Exception($obj['error']);
		}
		
		return $obj;
		
	}
	
	/**
	 * Delete messages
	 */
	public function deleteMessages($uuids) {
		
		$this->_validate();
		
		return $this->_mbox('deleteMessages',$uuids);
		
	}
	
	
	/**
	 * Mark a message as read
	 */
	public function markRead($uuid) {
		
		$this->_validate();
		
		return $this->_mbox('setFlags',array('RO', $uuid));
		
	}

	/**
	 *	Get a single message
	 */
	public function getMessage($uuid) {
		
		$this->_validate();
		
		return $this->_mbox('getMessage',array($uuid));
		
	}

	/**
	 * Get list of all messages
	 */
    public function getMessages() {

        // Validation
        $this->_validate();

        // Filter result
        return $this->applyFilter($this->_mbox('getMessages'), 0, -1, null, null);

    }
	
    /**
     * Get the number of unread messages
     */
	public function getUnreadCount() {

        // Validation
        $this->_validate();

		return array('unreadcount' => $this->_mbox('getUnreadCount'));	
	}
}

